<?php
loadHelper('database');
/**
 * Array(
 *  'name' => 'abc',
 *  'age' => 20
 * )
 * @param type $info
 */
function update_profile($info, $uid = '')
{
    $sql = "UPDATE `profiles` SET ";
    foreach($info as $key => $value)
    {
        $sql .= "`{$key}` = '{$value}',";
    }
    $sql = substr($sql, 0, strlen($sql) - 1) . " WHERE `UserId` = '{$uid}'";
    return mysqli_query(getDB(), $sql);
    //SAVE TO DB
}

function get_profile($uid)
{
    $sql = "SELECT * FROM `profiles` WHERE UserId = '{$uid}' LIMIT 1";
    $result = mysqli_query(getDB(), $sql);
    if($result === false) return false;
    if(mysqli_num_rows($result) > 0)
    {
        while($row = mysqli_fetch_assoc($result))
        {
            return $row;
        }
    }
    return false;
}
function get_entries($UId)
{
	$sql="SELECT * FROM `entries` WHERE `UserId`='$UId'";
	$result=mysqli_query(getDB(), $sql);
	$nums=mysqli_num_rows($result);
	$entries=array();
	if($nums>0)
	{
		while($rows=mysqli_fetch_assoc($result))
		{
			$entries[]= $rows['EntryId'];
		}
		
	}
	return $entries;
}
function get_votes($IdEntry)
{
	$nums=0;
	$sql="SELECT `vote` FROM `entries` WHERE `EntryId`='$IdEntry' LIMIT 1";
	$result=mysqli_query(getDB(), $sql);
	$nums=mysqli_fetch_assoc($result);
	return $nums['vote'];
}
function get_all_votes($UId)
{
	$num_votes=0;
	$entries = array();
	$entries = get_entries($UId);
	if($entries!=null)
	{
		foreach($entries as $IdEntry)
		{
			$num_votes = $num_votes + get_votes($IdEntry);
		}
	}
	return $num_votes;
}
?>